package com.itheima.a09threadsafe1;

public class MyThread extends Thread {
    //静态，表示这个类所有的对象，都共享ticket数据
    static int ticket = 0;
    @Override
    public void run() {

        while(true){
            synchronized (MyThread.class) { //同步代码块
                if (ticket < 100) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(Thread.currentThread().getName() + "@" + ticket);
                } else {
                    break;
                }
            }
        }
    }
}
